<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class comment_controller extends PZ_Controller{
	public function __construct(){
		parent::__construct();
		$this->authfunctions = array('addcomment','addalbumcomment','addstorecomment','deletealbumcomment','deletestorecomment','deletecomment');
	}

	public function addcomment(){
		if(!$this->sysperm['allowcomment']){
			$this->ajax_failed_response(lang('nopermission','tip'));
			return;
		}
		$comment = delete_html($this->param('comment'));
		$share_id = $this->param('id');
		$fvalidation = load_class('Form_validation');
		$fvalidation->set_rules('comment', 'lang:comment:field' , 'trim|required|max_length[210]|xss_clean');
		if ($fvalidation->run() == FALSE){
			$this->ajax_failed_response($fvalidation->error_string());
			return FALSE;
		}

		$shareapi = load_api('share');
		$share = $shareapi->getShareById($share_id);
		if (!$comment||!$share) {
			$this->ajax_failed_response(lang('500_postfaild','tip'));
			return;
		}
		$puzzcode = load_api('puzzcode');
		$at_array = $puzzcode->parse_at($comment);
		$new_comment['comment_txt'] = $at_array['message'];
		$new_comment['search_en'] = ' '.$at_array['atsearch_str'];
		$new_comment['share_id'] = $share_id;
		$new_comment['user_id'] = $this->cuser['uid'];
		$new_comment['create_time'] = TIMESTAMP;

		$commentapi = load_api('comment');
		if ($new_comment['comment_id'] = $commentapi->addcomment($new_comment)) {
			$new_comment['nickname'] = $this->cuser['nickname'];
			$new_comment['user_avatar'] = useravatar($this->cuser['uid'], 'middle');
			$new_comment['post_time_friend'] = pgmdate(TIMESTAMP);
			$new_comment['comment_txt'] = $puzzcode->parse_message($new_comment['comment_txt'],null,true);
			$new_comment['delete'] = true;

			if($this->cuser['uid']!=$share['user_id']){
				load_api('event')->postcomment($this->cuser['uid'],$share['title'],$share_id,$share['user_id']);
			}
			$this->ajax_success_response($new_comment, lang('200_postsuccess','tip'));
			return ;
		}else{
			$this->ajax_failed_response(lang('500_postfaild','tip'));
			return ;
		}
	}

	public function addalbumcomment(){
		if(!$this->sysperm['allowcomment']){
			$this->ajax_failed_response(lang('nopermission','tip'));
			return;
		}
		$comment = delete_html($this->param('comment'));
		$albumid = $this->param('id');
		$fvalidation = load_class('Form_validation');
		$fvalidation->set_rules('comment', 'lang:comment:field' , 'trim|required|max_length[210]|xss_clean');
		if ($fvalidation->run() == FALSE){
			$this->ajax_failed_response($fvalidation->error_string());
			return FALSE;
		}

		$albumapi = load_api('album');
		$album = $albumapi->findAlbumByid($albumid);
		if (!$comment||!$album) {
			$this->ajax_failed_response(lang('500_postfaild','tip'));
			return;
		}
		$new_comment['comment_txt'] = $comment;
		$new_comment['album_id'] = $albumid;
		$new_comment['user_id'] = $this->cuser['uid'];
		$new_comment['create_time'] = TIMESTAMP;

		$commentdb = load_db('comment/albumcomment');
		if ($new_comment['comment_id'] = $commentdb->add_one($new_comment)) {
			$new_comment['nickname'] = $this->cuser['nickname'];
			$new_comment['user_avatar'] = useravatar($this->cuser['uid'], 'middle');
			$new_comment['post_time_friend'] = pgmdate(TIMESTAMP);
			$new_comment['comment_txt'] = $new_comment['comment_txt'];
			$new_comment['delete'] = true;
			$this->ajax_success_response($new_comment, lang('200_postsuccess','tip'));
			return ;
		}else{
			$this->ajax_failed_response(lang('500_postfaild','tip'));
			return ;
		}
	}

	public function addstorecomment(){
		if(!$this->sysperm['allowcomment']){
			$this->ajax_failed_response(lang('nopermission','tip'));
			return;
		}
		$comment = delete_html($this->param('comment'));
		$storeid = $this->param('id');
		$fvalidation = load_class('Form_validation');
		$fvalidation->set_rules('comment', 'lang:comment:field' , 'trim|required|max_length[210]|xss_clean');
		if ($fvalidation->run() == FALSE){
			$this->ajax_failed_response($fvalidation->error_string());
			return FALSE;
		}

		$store = load_db('store')->find(array('store_id'=>$storeid));
		if (!$comment||!$store) {
			$this->ajax_failed_response(lang('500_postfaild','tip'));
			return;
		}
		$new_comment['comment_txt'] = $comment;
		$new_comment['store_id'] = $storeid;
		$new_comment['user_id'] = $this->cuser['uid'];
		$new_comment['create_time'] = TIMESTAMP;

		$commentdb = load_db('comment/storecomment');
		if ($new_comment['comment_id'] = $commentdb->add_one($new_comment)) {
			$new_comment['nickname'] = $this->cuser['nickname'];
			$new_comment['user_avatar'] = useravatar($this->cuser['uid'], 'middle');
			$new_comment['post_time_friend'] = pgmdate(TIMESTAMP);
			$new_comment['comment_txt'] = $new_comment['comment_txt'];
			$new_comment['delete'] = true;
			$this->ajax_success_response($new_comment, lang('200_postsuccess','tip'));
			return ;
		}else{
			$this->ajax_failed_response(lang('500_postfaild','tip'));
			return ;
		}
	}

	public function deletealbumcomment(){
		$commentid = $this->param('cid');
		$albumid = $this->param('aid');
		if (!is_numeric($commentid)||!is_numeric($albumid)) {
			$this->ajax_failed_response(lang('500_operatefaild','tip'));
			return;
		}
		$comment['comment_id'] = $commentid;
		$comment = load_db('comment/albumcomment')->find($comment);
		$album = load_db('album')->find(array('album_id'=>$albumid));
		if(!$this->isadmin&&!$this->iseditor&&$comment['user_id']!=$this->cuser['uid']&&$album['user_id']!=$this->cuser['uid']){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}
		if(load_db('comment/albumcomment')->delete($comment)){
			$this->ajax_success_response(null, lang('200_operatesuccess','tip'));
			return;
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
	}

	public function deletestorecomment(){
		$commentid = $this->param('cid');
		$storeid = $this->param('storeid');
		if (!is_numeric($commentid)||!is_numeric($storeid)) {
			$this->ajax_failed_response(lang('500_operatefaild','tip'));
			return;
		}
		$comment['comment_id'] = $commentid;
		$comment = load_db('comment/storecomment')->find($comment);
		$store = load_db('store')->find(array('store_id'=>$storeid));
		if(!$this->isadmin&&!$this->iseditor&&$comment['user_id']!=$this->cuser['uid']&&$store['user_id']!=$this->cuser['uid']){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}
		if(load_db('comment/storecomment')->delete($comment)){
			$this->ajax_success_response(null, lang('200_operatesuccess','tip'));
			return;
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
	}

	public function deletecomment(){
		$hash = $this->param('hash');
		$share_id = $this->param('sid');
		if (!is_numeric($hash)||!is_numeric($share_id)) {
			$this->ajax_failed_response(lang('500_operatefaild','tip'));
			return;
		}
		$comment['comment_id'] = $hash;
		$comment['share_id'] = $share_id;
		$comment = load_db('comment')->find($comment);
		if(!$this->isadmin&&!$this->iseditor&&$comment['user_id']!=$this->cuser['uid']){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}

		if(load_api('comment')->delcomment($comment)){
			$this->ajax_success_response(null, lang('200_operatesuccess','tip'));
			return;
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
	}

	public function getcomments(){
		$page = $this->param('page','1');
		if($shareid = $this->param('shareid')){
			$commentapi = load_api('comment');
			$layoutsetting = load_api('setting')->getSetting('layoutdetail');
			$commentperpage = ($layoutsetting['uidetail_comment_pagenum'])?$layoutsetting['uidetail_comment_pagenum']:5;
			$rs = $commentapi->getcommentsbyshare($shareid,$page,$commentperpage);
			$comments = array();
			foreach ($rs as $comment) {
				$comment['post_time_friend'] = pgmdate($comment['create_time']);
				$comment['user_avatar'] = useravatar($comment['user_id'], 'middle');
				$comment['comment_txt'] = load_api('puzzcode')->parse_message($comment['comment_txt'],null,true);
				$comment['delete'] = ($comment['user_id']==$this->cuser['uid']||$this->isadmin||$this->iseditor);
				$comments[] = $comment;
			}
			$this->ajax_success_response($comments, '');
			return;
		}
		$this->ajax_failed_response(lang('500_getcommentfaild','tip'));
	}

	public function getalbumcomments(){
		$page = $this->param('page','1');
		if($aid = $this->param('aid')){
			$albumcomdb = load_db('comment/albumcomment');
			$album = load_db('album')->find(array('album_id'=>$albumid));
			$rs = $albumcomdb->search(array('album_id'=>$aid),$page,5);
			$comments = array();
			foreach ($rs as $comment) {
				$comment['post_time_friend'] = pgmdate($comment['create_time']);
				$comment['user_avatar'] = useravatar($comment['user_id'], 'middle');
				$comment['comment_txt'] = $comment['comment_txt'];
				$comment['delete'] = ($comment['user_id']==$this->cuser['uid']||$this->isadmin||$this->iseditor||$this->cuser['uid']==$album['user_id']);
				$comments[] = $comment;
			}
			$this->ajax_success_response($comments, '');
			return;
		}
		$this->ajax_failed_response(lang('500_getcommentfaild','tip'));
	}


	public function getstorecomments(){
		$page = $this->param('page','1');
		if($storeid = $this->param('storeid')){
			$storecomdb = load_db('comment/storecomment');
			$store = load_db('store')->find(array('store_id'=>$storeid));
			$rs = $storecomdb->search(array('store_id'=>$storeid),$page,5);
			$comments = array();
			foreach ($rs as $comment) {
				$comment['post_time_friend'] = pgmdate($comment['create_time']);
				$comment['user_avatar'] = useravatar($comment['user_id'], 'middle');
				$comment['comment_txt'] = $comment['comment_txt'];
				$comment['delete'] = ($comment['user_id']==$this->cuser['uid']||$this->isadmin||$this->iseditor||$this->cuser['uid']==$store['user_id']);
				$comments[] = $comment;
			}
			$this->ajax_success_response($comments, '');
			return;
		}
		$this->ajax_failed_response(lang('500_getcommentfaild','tip'));
	}

}
